/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.dona.imsidetector.adpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.dona.imsidetector.R;
import com.dona.imsidetector.db.entity.BwList;
import com.dona.imsidetector.fragment.BlackWhiteFragment;
import com.dona.imsidetector.utils.MMKVUtils;

import java.text.SimpleDateFormat;
import java.util.List;

public class ImsiBlackWhiltListAdapter extends RecyclerView.Adapter<ImsiBlackWhiltListAdapter.ViewHolder> {

    private static final String TAG = "ListAdapter";
    private List<BwList> bwListList;

    private LayoutInflater layoutInflater;

    private BlackWhiteFragment context;

    private boolean multChoice = false;


    private View prevClickView;


    public ImsiBlackWhiltListAdapter(BlackWhiteFragment context, List<BwList> bwLists){
        this.context = context;
        this.bwListList = bwLists;
        layoutInflater = LayoutInflater.from(context.getContext());
    }


    public void setViewSelected(boolean selected, View view){
        if(selected){
            view.setBackgroundColor(Color.WHITE);
            view.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_select));
        }else {
            view.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_normal));
        }

    }




    public void itemSelect(ViewHolder holder){
        if(multChoice) {

        }else {
            if(null != prevClickView){
                if(prevClickView.equals(holder.imsiView)){
                    setViewSelected(false, holder.imsiView);
                    context.deleteOperationsList(holder.bwList);
                    prevClickView = null;
                    Log.e(TAG, "after set null: prevClickView : " + prevClickView);
                }else {
                    setViewSelected(true, holder.imsiView);
                    setViewSelected(false,prevClickView);
                    if(context.operationsList.size() == 0){
                        context.addOperationsList(holder.bwList);
                    }else {
                        context.operationsList.clear();
                        context.addOperationsList(holder.bwList);
                    }
                    prevClickView = holder.imsiView;
                }
            }else {
                setViewSelected(true, holder.imsiView);
                prevClickView = holder.imsiView;
                if(context.operationsList.size() == 0){
                    context.addOperationsList(holder.bwList);
                }else {
                    context.operationsList.clear();
                    context.addOperationsList(holder.bwList);
                }
            }
            context.updateImsiItems(holder.bwList.getUuid());
            MMKVUtils.put("missionName",holder.bwList.getTitle());
            MMKVUtils.put("missionId",holder.bwList.getUuid());
            Log.e(TAG, "position: " + holder.index + "  itemSelect: " + holder.bwList.getTitle());
            Log.e(TAG, "toDeleteList: " + context.operationsList.size());
        }


    }



    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.adapter_black_white_imsi_list_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        Log.e("ListAdapter", "onCreateViewHolder: " + holder.toString() );
        holder.imsiView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.imsiView = v;
                itemSelect(holder);

            }
        });
        holder.imsiView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                BwList bwList = bwListList.get(holder.index);
                Log.e(TAG, "onLongClick: " + bwList);
//                new MaterialDialog.Builder(context)
//                        .content("确定删除名单：" + imsiList.getTitle()+ "吗？")
//                        .positiveText("确定")
//                        .negativeText("取消")
//                        .onPositive(new MaterialDialog.SingleButtonCallback() {
//                            @Override
//                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                String msg = ListDao.delete(imsiList);
//                                if(msg == null){
//                                    context.updateImsiLists();
//                                }
//                                Log.e(TAG, "ListDao.delete: " + msg);
//                            }
//                        })
//                        .show();
//                context.setListBottomNavigation();
                holder.imsiView = v;
                itemSelect(holder);
                return true;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        BwList bwList = bwListList.get(position);
        Log.e("onBindViewHolder", "position: " + position );
        holder.index = position;
        holder.tvTitle.setText(bwList.getTitle());
        holder.bwList = bwList;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String dateString = formatter.format(bwList.getCreate_time());
        holder.tvCreatTime.setText(dateString);
    }

    @Override
    public int getItemCount() {
        return bwListList.size();
    }

    public boolean isMultChoice() {
        return multChoice;
    }

    public void setMultChoice(boolean multChoice) {
        multChoice = multChoice;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private static final String TAG = "ViewHolder";

        BwList bwList;

        View imsiView;
        TextView tvTitle;
        TextView tvCreatTime;
        public int index;

        public ViewHolder(View view){
            super(view);
            imsiView = view;

            tvTitle = (TextView)view.findViewById(R.id.imsi_title);
            tvCreatTime = (TextView)view.findViewById(R.id.imsi_creat_time);
        }


    }
}
